
cap cd  "C:\Users\ejm5\Dropbox\RCT on corruption & productivity\Replication"
cap cd "C:\Users\yx42\Dropbox\RCT on corruption & productivity\Replication"
cap cd "C:\Users\trfet\Dropbox\research\governance\RCT on corruption & productivity\Replication"
use "New_Comprehensive_August_30_2023.dta", clear

/************************************************************************************************/
drop if b2c>50
drop if b2b>20

generate restaurant=1 if stra_sector=="Hospitality & Tourism"|stra_sector=="Hotels"|stra_sector=="Hotels & Restaurants"|stra_sector=="Restaurants"|stra_sector=="Food"
replace restaurant=0 if restaurant==.


/*Corruption*/

* j7a: % of annual sales paid in informal payments
* missing in 35,000 (about 17%)
* about 12% of values are in this set: -9 DK, -8 refuse, and a few random -7 and -4 that don't have corresponding codes
* 529 are in (0,1)
* 20,000 are [1,100]
* 117,000 are ==0
describe j7a 								
replace j7a=. if j7a<0 	
replace j7a=j7a*100 if j7a<1 & j7a>0	
sum j7a, detail
/*by country, sort: egen mean_j7a=mean(j7a)
replace j7a=mean_j7a if j7a==.*/

* j3: Inspected by tax officials last 12 months?
* missing or DK or -8 (refuse?) in about 2400
* 1 yes, 2 no. There's one zero which we interpret as no.
* Note, about 51-52% yes. So this is not rare.
generate inspection=1 if j3==1
replace inspection=0 if j3==2|j3==0

*j5: in any of these inspections was a gift or informal payment requested
* Missing for 93,000; DK, refuse or unexplained -4 or 0 for another 5000.
* 1 yes, 2 no. There's one zero which we interpret as no.
* We also enter no if j3 is no (ie not inspected). So be sure to interpret this one as not conditional. 
generate bribe_inspection=0 if j5==2|j5==0
replace bribe_inspection=1 if bribe_inspection==.
replace bribe_inspection=0 if inspection==0

* j4: Number of tax inspections last 12 months?
* 92k missing, 5k <0 (DK/refuse/etc), 513 mysteriously zero
* right-skewed with most mass in [1,6]; spikes at 12, 20, 24, 30
* ranges up to 3,123
* p95 = 12 and p99 = 24. 
* we winsorize at 12
codebook j4, tab(120)
summ j4 if j4>=0, det
gen inspection_count = j4
replace inspection_count=. if j4<0
gen insp_count_wins = inspection_count
replace insp_count_wins = 12 if insp_count_wins > 12
la var insp_count_wins "# of times inspected by tax authority last 12 mos, winsorized at 12"
la var inspection_count "# of times inspected by tax authority last 12 mos, ranges up to 3,123"

/*Regulation*/
* j2: % of sr mgt time dealing with govt regulations
* missing for 15k obs (9%)l zero for 64k (35% or so)
* ranges to 100% for a few ornery people
replace j2=. if j2<0
winsor2 j2											// this doesn't actually change anything

/***summarize variables for tfp***/

**sales 
describe d2
sum d2, d
tab d2 if d2<0 										// Note: some -8 and -7 that don't appear in codebook but are likely not real #s

**intermdiate inputs
describe n2e
sum n2e, d
tab n2e if n2e<0 									// Note: some -8 and -7 that don't appear in codebook but are likely not real #s

**labor inputs
describe n2a
sum n2a, d
tab n2a if n2a<0									// same

**capital stock
describe n6a n6b
sum n6a, d
sum n6b, d

************check the basic quality of data*************
replace d2 =. if d2 == -9 |d2==-8							// suggest all of these change if <0 instead
replace n2e=. if n2e == -9  |n2e ==-8
replace n2a=. if n2a == -9 |n2a ==-8
replace n6a=. if n6a == -9 |n6a ==-8

***cost share (intermediate inputs / sales)
*gen l_share = n2a/d2
gen m_share = n2e/d2

*sum l_share m_share, d

**generate value-added
gen va = d2 - n2e


**use only obs that these shares are between 0 and 1**
gen baseline_sample=1 if m_share>0&m_share<1

**value-added per workers (L1 = number of permanent FT employees)
gen lprod = log(va/l1) /*if baseline_sample == 1*/

**compute sectoral average share
gen l_share = n2a/va
egen ml_sh = mean(l_share) if baseline_sample==1, by (isic_v3_1)
replace ml_sh = . if ml_sh<0|ml_sh>1

**adjust markup to compute capital share										// what's the idea here? Why is 0.8 hard coded?
gen al = ml_sh/0.8
sum al, d
gen ak = 1 - al


**adjust for variable inputs
gen lnv = log(va)
gen lnl = log(l1)
gen lnk = log(n6a+n6b)

gen TFP = lnv/0.8 - al*lnl - ak*lnk if baseline_sample == 1

 replace bribe_inspection=0 if inspection==0
 codebook d30b l30a
 replace l30a=. if l30a<0
 replace d30b=. if d30b<0
 
generate l_d2=ln((d2+1)/l1)
replace f1=0 if f1<0
replace f1=100 if f1==105
generate quality_certifictaion=1 if b8==1
replace  quality_certifictaion=0 if b8!=1


/********************************************************************************************/

/*Fixed Effects*/
split country, generate(country) parse(2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023) limit(1)
split country, generate(year) parse(0) limit(3)
rename country country_yr
replace year2="10" if year2=="1"
replace year2="20" if year2=="2"
replace year2="5" if year3=="6"
replace year2="6" if year3=="6"
replace year2="7" if year3=="7"
replace year2="8" if year3=="8"
replace year2="9" if year3=="9"
drop year1
rename country1 country
rename year2 year
replace country="Central African Republic" if country=="Centralafricanrepublic"
encode country, gen(country_code)
destring year, replace

encode stra_sector, gen(sector2)
encode country_yr, gen(country_yr_code)
/************************************************************************************************/

/*Management Quality*/
/*Management Quality: Including: performance monitoring (information collection and analysis); target-setting (the use of short- and long-run targets) and incentives (reward high-performing employees; and retraining or removing underperformers).*/
describe r*

mvdecode r*, mv(-9=.c\-7=.b)

replace r1=0 if r1==4
replace r2=2-r2
replace r3=0 if r2==0
replace r4=2-r4
replace r5=0 if  r4==0
replace r6=0 if  r4==0
/*replace r6=7-r6*/
replace r7=0 if  r4==0
replace r8=2-r8
replace r9=0 if r8==0
replace r10=5-r10
replace r11=4-r11

/*Factor Analysis*/
log using "tables\Table1.smcl", replace
factor r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11
predict management_quality
log close

sum management_quality
preserve
collapse management_quality, by(country_code)
hilo management_quality country_code
restore
/************************************************************************************************/

/************************************************************************************************/
binscatter j7a management_quality, absorb(country_code) nquantiles(20) reportreg xtitle("Management Quality Index", size(medium) margin(medsmall)) ytitle("Bribe Payments/Total Sales (%)", size(medium) margin(medsmall)) note("n=17,293 Beta= -.135; SE=.044", size(small)  position(7)) title("Full Sample", size(medium))
graph save "graph\binscatter_management_all.gph", replace
graph export "graph\Figure2_bins.pdf", as(pdf) replace

binscatter j7a management_quality if stra_sector=="Hospitality & Tourism"|stra_sector=="Hotels"|stra_sector=="Hotels & Restaurants"|stra_sector=="Restaurants"|stra_sector=="Food", absorb(country_code) nquantiles(20) reportreg xtitle("Management Quality Index", size(medium) margin(medsmall)) ytitle("Bribe Payments/Total Sales (%)", size(medium) margin(medsmall)) note("n=2,390 Beta= -.277; SE=.126", size(small)  position(7)) title("Only Restaurants", size(medium))
graph save "graph\binscatter_management_restaurant.gph", replace


graph combine "graph\binscatter_management_all.gph" "graph\binscatter_management_restaurant.gph", rows(1) xcommon ycommon imargin(tiny)
graph export "graph\Bins.pdf", as(pdf) replace

/************************************************************************************************/


/*Table 2: Outcome Varibale is Bribery and treatment variable is Management*/
reg j7a management_quality if b2c<=0 & b2b<=0, cluster(year)
outreg2 using "tables\Table2", bdec(3) tdec(3) e(rmse) replace ctitle("Unadjusted") addtext("Country FE", No, "Year FE", No, "Size FE", No , "Industry FE", No)
areg j7a management_quality if b2c<=0 & b2b<=0, cluster(year)  absorb(country_code)
outreg2 using "tables\Table2", bdec(3) tdec(3) e(rmse) ctitle("Country FE") addtext("Country FE", Yes, "Year FE", No, "Size FE", No , "Industry FE", No)
areg j7a management_quality i.year if b2c<=0 & b2b<=0, cluster(year)  absorb(country_code)
outreg2 using "tables\Table2", bdec(3) tdec(3) e(rmse) ctitle("Year FE") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", No , "Industry FE", No)
areg j7a management_quality i.year i.size if b2c<=0 & b2b<=0, cluster(year)  absorb(country_code)
outreg2 using "tables\Table2", bdec(3) tdec(3) e(rmse) ctitle("Size FE") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", No)
areg j7a management_quality i.year i.size i.sector2 if b2c<=0 & b2b<=0, cluster(year)  absorb(country_code)
outreg2 using "tables\Table2", bdec(3) tdec(3) e(rmse) ctitle("Indusrt FE") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes)
areg j7a management_quality i.year i.size i.sector2 if stra_sector=="Hospitality & Tourism"|stra_sector=="Hotels"|stra_sector=="Hotels & Restaurants"|stra_sector=="Restaurants"|stra_sector=="Food" & b2c<=0 & b2b<=0, cluster(year)  absorb(country_code)
outreg2 using "tables\Table2", bdec(3) tdec(3) e(rmse) excel ctitle("Restaurants") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes)




/************************************************************************************************/

/*Table 3: Mechanims*/
areg lprod management_quality i.year i.size i.sector2 if b2c<=0 & b2b<=0 , cluster(year)  absorb(country_code)
outreg2 using "tables\Table3", bdec(3) tdec(3) e(rmse) replace	ctitle("Productivity") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes)

areg l_d2 management_quality i.year i.size i.sector2 if b2c<=0 & b2b<=0, cluster(year)  absorb(country_code)
outreg2 using "tables\Table3", bdec(3) tdec(3) e(rmse) 	ctitle("Revenue") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes)

areg f1 management_quality i.year i.size i.sector2 if b2c<=0 & b2b<=0, cluster(year)  absorb(country_code)
outreg2 using "tables\Table3", bdec(3) tdec(3) e(rmse) 	ctitle("Capacity (%)") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes)

areg j2 management_quality i.year i.size i.sector2 if b2c<=0 & b2b<=0, cluster(year)  absorb(country_code)
outreg2 using "tables\Table3", bdec(3) tdec(3) e(rmse) 	ctitle("Time Spent on Regulations (%)") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes)

areg quality_certifictaion management_quality i.year i.size i.sector2 if b2c<=0 & b2b<=0, cluster(year)  absorb(country_code)
outreg2 using "tables\Table3", bdec(3) tdec(3) e(rmse) 	ctitle("Quality Certification") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes) excel
/************************************************************************************************/

/*Table B3: Mechanims Restaurant*/
preserve
keep if  stra_sector=="Hospitality & Tourism"|stra_sector=="Hotels"|stra_sector=="Hotels & Restaurants"|stra_sector=="Restaurants"|stra_sector=="Food"

areg lprod management_quality i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB3", bdec(3) tdec(3) e(rmse) replace	ctitle("Productivity") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes)

areg l_d2 management_quality i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB3", bdec(3) tdec(3) e(rmse) 	ctitle("Revenue") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes)

areg f1 management_quality i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB3", bdec(3) tdec(3) e(rmse) 	ctitle("Capacity (%)") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes)

areg j2 management_quality i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB3", bdec(3) tdec(3) e(rmse) 	ctitle("Time Spent on Regulations (%)") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes)

areg quality_certifictaion management_quality i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB3", bdec(3) tdec(3) e(rmse) 	ctitle("Quality Certification") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes) excel
restore
/************************************************************************************************/

*Table B4:
/*Types of Management*/

areg j7a r1   i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB4", bdec(3) tdec(3) e(rmse N_clust) replace ctitle("Problem Solving") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes) 

areg j7a r2   i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB4", bdec(3) tdec(3) e(rmse N_clust) ctitle("Any Performance Indicators") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes) 

areg j7a r3   i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB4", bdec(3) tdec(3) e(rmse N_clust) ctitle("Number of Performance Indicators") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes) 

areg j7a r4   i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB4", bdec(3) tdec(3) e(rmse N_clust) ctitle("Any Production Targets") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes) 

areg j7a r5   i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB4", bdec(3) tdec(3) e(rmse N_clust) ctitle("Time Frame of Production Targets") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes) 

areg j7a r6   i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB4", bdec(3) tdec(3) e(rmse N_clust) ctitle("Who Knew Of production Targets") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes) excel


*Table B5:
/*Types of Management*/

areg j7a r7   i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB5", bdec(3) tdec(3) e(rmse N_clust) ctitle("Ease of Achieveing Targets") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes) replace

areg j7a r8   i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB5", bdec(3) tdec(3) e(rmse N_clust) ctitle("Performance Bonuses Based on Targets") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes) 

areg j7a r9   i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB5", bdec(3) tdec(3) e(rmse N_clust) ctitle("Performance Bonuses") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes) 

areg j7a r10   i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB5", bdec(3) tdec(3) e(rmse N_clust) ctitle("Promotion of Non-Managers") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes) 

areg j7a r11   i.year i.size i.sector2, cluster(year)  absorb(country_code)
outreg2 using "tables\Appendix_TableB5", excel bdec(3) tdec(3) e(rmse N_clust) ctitle("Reassignment of Under-Performers") addtext("Country FE", Yes, "Year FE",Yes, "Size FE", Yes, "Industry FE", Yes) 


